package com.marco.polo.travel.controller;

import com.marco.polo.travel.model.Message;
import com.marco.polo.travel.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private MessageService service;

    @RequestMapping("/home")
    public String home() {

        return "index";
    }

    @RequestMapping("/about")
    public String about() {

        return "about";
    }

    @RequestMapping("/typography")
    public String typography() {

        return "typography";
    }

    @RequestMapping("/gallery")
    public String gallery() {

        return "gallery";
    }

    @RequestMapping("/blog")
    public String blog() {

        return "blog";
    }

    @RequestMapping("/contact")
    public String contact() {

        return "contact";
    }

    @RequestMapping("/single")
    public ModelAndView single() {
        List<Message> message = service.getMessage();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("single");
        modelAndView.addObject("message", message);
        modelAndView.addObject("total", message.size());
        return modelAndView;
    }

    @RequestMapping("/message")
    public String message(Message message) {
        if(message!=null && !message.getComment().equals("") && !message.getNickname().equals("")){
            message.setCreateTime(new Date());
            service.insertMessage(message);
        }
        return "redirect:single";
    }
}
